package com.qibo.baselib.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;


import java.util.ArrayList;


/**
 * Created by zhuzidong
 * on 2017/10/10.
 * Email:591079255@qq.com
 */

public abstract class BaseFragment extends Fragment
{
    private static final String CONSTANT_TYPE_STRING = "constant_string";
    private static final String CONSTANT_TYPE_INTEGER = "constant_integer";
    private static final String CONSTANT_TYPE_BOOLEAN = "constant_boolean";
    private static final String CONSTANT_TYPE_ARRAYLIST = "constant_arraylist";
    private static final String CONSTANT_TYPE_BUNDLE = "constant_bundle";

    private View mView;
    private FragmentSwitchListener mListener;
    /**上下文*/
    public Context mContext;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        if (isNeedInflaterStatusBar())
        {
            addStatusView(setStatusBarColor());
        }

        if (isNeedRemoveStatusBar())
        {
            removeStatusView();
        }
        if (mListener != null)
        {
            mListener.viewInflatedFinished(true);
        }
        mView = inflater.inflate(getInflaterLayoutId(),container,false);
        mContext = getActivity();
        initBeforeStart();
        initView(mView);
        initAction();
        initData();
        return mView;
    }

    protected void initBeforeStart()
    {

    }

    protected abstract int getInflaterLayoutId();

    protected abstract void initView(View view);

    protected abstract void initAction();

    protected abstract void initData();

    protected void  showLoading()
    {
        if (getActivity() != null)
        {
        }
    }
    protected boolean isNeedInflaterStatusBar()
    {
        return false;
    }

    protected boolean isNeedRemoveStatusBar()
    {
        return false;
    }

    protected int setStatusBarColor()
    {
        return Color.parseColor("#FFFFFF");
    }

    protected View getCurrentView()
    {
        return mView;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    private void addStatusView(int statusColor)
    {
        int statusHeight = getStatusBarHeight(getActivity());
        Window window = getActivity().getWindow();
        FrameLayout decr = (FrameLayout) window.getDecorView();
        View statusView = decr.getChildAt(0);
        if (statusView.getLayoutParams().height == statusHeight )
        {
            statusView.setBackgroundColor(statusColor);
            return;
        }else
        {
            ViewGroup viewGroup = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
            View contentView = viewGroup.getChildAt(0);
            FrameLayout.LayoutParams viewGroupLayoutParams = (FrameLayout.LayoutParams) contentView.getLayoutParams();
            viewGroupLayoutParams.topMargin += statusHeight;
            contentView.setLayoutParams(viewGroupLayoutParams);
            View newStatusView = new View(getActivity());
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusHeight);
            newStatusView.setBackgroundColor(statusColor);
            newStatusView.setLayoutParams(layoutParams);
            decr.addView(newStatusView,0);
        }
    }

    private void removeStatusView()
    {
        int statusHeight = getStatusBarHeight(getActivity());
        FrameLayout decor = (FrameLayout) getActivity().getWindow().getDecorView();
        View statusView = decor.getChildAt(0);
        if (statusView.getLayoutParams().height == statusHeight)
        {
            decor.removeViewAt(0);
            ViewGroup viewGroup = (ViewGroup) getActivity().findViewById(Window.ID_ANDROID_CONTENT);
            View contentView = viewGroup.getChildAt(0);
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) contentView.getLayoutParams();
            lp.topMargin -= statusHeight;
            contentView.setLayoutParams(lp);
        }

    }

    public void setFragmentSwitchListener(FragmentSwitchListener listener)
    {
        this.mListener = listener;
    }

    public static int getStatusBarHeight(Context context)
    {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    public void switchActivity(Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        startActivity(intent);
    }

    public void switchActivityForResult(int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        startActivityForResult(intent,requestCode);
    }

    public void switchActivityWithBundle(Bundle bundles, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_BUNDLE,bundles);
        startActivity(intent);
    }

    public void switchActivityWithBundleForResult(int requestCode, Bundle bundles, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_BUNDLE,bundles);
        startActivityForResult(intent,requestCode);
    }


    public void switchActivityWithInteger(int data,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_INTEGER,data);
        startActivity(intent);
    }

    public void switchActivityWithIntegerForResult(int data,int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_INTEGER,data);
        startActivityForResult(intent,requestCode);
    }

    public void switchActivityWithString(String data, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_STRING,data);
        startActivity(intent);
    }

    public void switchActivityWithStringForResult(String data, int requestCode, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_STRING,data);
        startActivityForResult(intent,requestCode);
    }

    public void switchActivityWithBoolean(boolean data,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_BOOLEAN,data);
        startActivity(intent);
    }

    public void switchActivityWithBooleanForResult(boolean data,int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_BOOLEAN,data);
        startActivityForResult(intent,requestCode);
    }

    public void switchActivityWithArrayList(ArrayList data, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_ARRAYLIST,data);
        startActivity(intent);
    }

    public void switchActivityWithArrayListForResult(ArrayList data, int requestCode, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(getActivity(),typeClass);
        intent.putExtra(CONSTANT_TYPE_ARRAYLIST,data);
        startActivityForResult(intent,requestCode);
    }


    public interface FragmentSwitchListener
    {
        public void viewInflatedFinished(boolean finished);
    }
}
